package com.xyhy.core.utils.db;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class DBUtil {
  public static String likeClear(String name) {
    name = Optional.ofNullable(name).orElse("");
    name = name.replaceAll("\\\\", "\\\\");
    name = name.replaceAll("%", "\\\\%");
    return Optional.ofNullable(name).orElse("");
  }

  public static String startWith(String name) {
    return likeClear(name) + "%";
  }

  public static String endWith(String name) {
    return "%" + likeClear(name);
  }

  public static String contain(String name) {
    return "%" + likeClear(name) + "%";
  }

  public static String upperFirstCase(String str) {
    if (StringUtils.isNotEmpty(str)) { // 连续下划线
      char[] chars = str.toCharArray();
      chars[0] = Character.toUpperCase(chars[0]);
      return String.valueOf(chars);
    }
    return "";
  }

  public static String getDbColumn(String property) {
    return String.format("f_%s", StrUtil.toUnderlineCase(property));
  }

}
